<?php

namespace App\Repositories;
use Cache;
use App\Models\JzMatchRelation;
use App\Repositories\Repository;

class JzMatchRelationRepository extends Repository{

    static $tag = 'testing.match.relation';


    public function getByOid($oid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.oid');
        $cate = $this->remember($cacheKey.$oid, function () use ($oid) {
            return JzMatchRelation::where('oid', $oid)
                ->orderBy('id', 'desc')
                ->first();
        });
        return $cate;
    }

    public function forgretgetByOid($oid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.oid');
        $this->forget($cacheKey.$oid);
    }


    public function storeMatch($data){
       return JzMatchRelation::insertGetId($data);
    }


    public function matchByUidTid($uid,$tid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.uidTid');
        $cate = $this->remember($cacheKey.$uid.$tid, function () use ($uid,$tid) {
            return JzMatchRelation::where('uid', $uid)
                ->where('tid',$tid)
                ->orderBy('id', 'desc')
                ->first();
        });
        return $cate;
    }

    public function forgretmatchByUidTid($uid,$tid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.uidTid');
        $this->forget($cacheKey.$uid.$tid);
    }

    /**获取oid
     * @return mixed
     */
    public function getUserByAppid($appid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.appid');
        $cate = $this->remember($cacheKey.$appid, function () use($appid){
                return JzMatchRelation::where('appid',$appid)
                    ->orderBy('id', 'desc')
                    ->pluck('oid');
        });
        return $cate;
    }

    /**清除获取oid缓存
     * @param $appid
     */
    public function forgretUserByAppid($appid){
        //清除缓存
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.appid');
        $this->forget($cacheKey.$appid);
    }

    /**清除获取oid缓存
     * @param $appid
     */
    public function forgetUserByTidAppid($appid,$tid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.tidAppid');
        $this->forget($cacheKey.$appid.$tid);
    }

    /**获取oid
     * @param $tid
     * @param $appid
     * @return mixed
     */
    public function getUserByTidAppid($tid,$appid){
        $cacheKey = getCacheKey('testing.cache_key.jzmatchrelation.tidAppid');
        $cate = $this->remember($cacheKey.$appid.$tid, function ()use($appid,$tid) {
            return JzMatchRelation::where('tid',$tid)
                ->orderBy('id', 'desc')
                ->where('appid',$appid)
                ->pluck('oid');
        });
        return $cate;
    }

    /**根据订单id 删除匹配关系
     * @param $oid
     */
    public function deleteMatch($oid,$appid){
        return  JzMatchRelation::where('oid',$oid)->where('appid',$appid)->delete();
    }


    public function model()
    {
        return app(JzMatchRelation::class);
    }

    public function tag()
    {
        return self::$tag;
    }

    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }
}